﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace yeubongda.Models
{
    public class YBDModels
    {
        public static List<MENU> DocmenuCha()
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from menu in ybd.MENUs
                        where menu.MAMNCHA == null
                        select menu;
            return query.ToList<MENU>();
        }
        public static MENU GetMenu(int id)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from menu in ybd.MENUs
                        where menu.MAMN == id
                        select menu;
            return query.First();
        }
        public static MENU GetMenuCha(int id)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query1 = from menu in ybd.MENUs
                        where menu.MAMN == id
                        select menu;

            var query = from menu in ybd.MENUs
                        where menu.MAMN == query1.FirstOrDefault().MAMNCHA
                         select menu;
            return query.First();
        }
        public static List<MENU> DocmenuCon(int MaMNCha)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from menu in ybd.MENUs
                        where menu.MAMNCHA == MaMNCha
                        select menu;
            return query.ToList<MENU>();
        }
        public static List<MENU> DocmenuCungCap(int id)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from menu in ybd.MENUs
                        where menu.MAMNCHA == id
                        select menu;
            return query.ToList<MENU>();
        }
        public static List<BAIBAO> DocBaiBao(int menu)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from baibao in ybd.BAIBAOs
                        where baibao.MaMN==menu
                        select baibao;
            return query.ToList<BAIBAO>();
        }
        public static int LaySoTrang(int soSanPhamMotTrang, int MaMN)
        {
            ybdEntities1 context = new ybdEntities1();
            var query = (from baibao in context.BAIBAOs
                         where baibao.MaMN == MaMN
                         select baibao).Count();
            int soTrang = query / soSanPhamMotTrang;
            if (query % soSanPhamMotTrang > 0)
                soTrang++;

            return soTrang;
        }
        public static List<BAIBAO> LayDanhSach(int maMenu, int page, int soSanPhamMotTrang)
        {
            ybdEntities1 context = new ybdEntities1();
            var query = (from baibao in context.BAIBAOs
                         where baibao.MaMN == maMenu
                         orderby baibao.MaMN descending
                         select baibao).Skip(page * soSanPhamMotTrang).Take(soSanPhamMotTrang);

            return query.ToList<BAIBAO>();
        }
        public static List<BAIBAO> DocBaiBaoMoiNhat()
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from baibao in ybd.BAIBAOs
                        orderby baibao.MABAI descending
                        select baibao;
            return query.ToList<BAIBAO>();
        }
        public static List<BAIBAO> DocBaiBaoMoiNhat(int id)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from baibao in ybd.BAIBAOs
                        where baibao.MaMNCHA != id
                        orderby baibao.MABAI ascending
                        select baibao;
            return query.ToList<BAIBAO>();
        }
        public static List<BAIBAO> DocBaiBaoMoiNhatTheoMenuCon(int id, int bb)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from baibao in ybd.BAIBAOs
                        where (baibao.MaMN == id) && (baibao.MABAI != bb)
                        orderby baibao.MABAI ascending
                        select baibao;
            return query.ToList<BAIBAO>();
        }
        public static List<BAIBAO> DocBaiBaoMoiNhatTheoMenu(int menu)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from baibao in ybd.BAIBAOs
                        where baibao.MaMNCHA==menu
                        orderby baibao.MABAI descending
                        select baibao;
            return query.ToList<BAIBAO>();
        }
        public static List<BAIBAO> DocBaiBaoMoiNhatTheoMenuCon(int menu)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from baibao in ybd.BAIBAOs
                        where baibao.MaMN == menu
                        orderby baibao.MABAI descending
                        select baibao;
            return query.ToList<BAIBAO>();
        }

        public static int LaySoTrangTenBB(int soSanPhamMotTrang, string tenBB)
        {
            ybdEntities1 context = new ybdEntities1();
            var query = (from baibao in context.BAIBAOs
                         where baibao.TENBAI.Contains(tenBB)
                         select baibao).Count();
            int soTrang = query / soSanPhamMotTrang;
            if (query % soSanPhamMotTrang > 0)
                soTrang++;

            return soTrang;
        }
        public static List<BAIBAO> getListByTenBaiBao(string tenBB, int page, int soSanPhamMotTrang)
        {
            ybdEntities1 context = new ybdEntities1();
            var query = (from bb in context.BAIBAOs
                         orderby bb.TENBAI descending
                         where bb.TENBAI.Contains(tenBB)
                        select bb).Skip(page * soSanPhamMotTrang).Take(soSanPhamMotTrang);
            
            return query.ToList<BAIBAO>();
        }

        public static List<LOAI> GetList()
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from loai in ybd.LOAIs
                        orderby loai.maloai descending
                        select loai;
            return query.ToList<LOAI>();
        }
        public static List<BAIBAO> GetListBB()
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from loai in ybd.BAIBAOs
                        orderby loai.MABAI descending
                        select loai;
            return query.ToList<BAIBAO>();
        }
        
        public static string TimUSER(string username, string password)
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from user in ybd.USERs
                        where user.TENUSER == username
                        where user.Password == password
                        select user;

            return query.First().TENUSER.ToString();

        }

        public static List<USER> DSUSER()
        {
            ybdEntities1 ybd = new ybdEntities1();
            var query = from user in ybd.USERs
                        select user;

            return query.ToList<USER>();

        }

        public static void Ghi(string entityname, object data)
        {
            ybdEntities1 ybd = new ybdEntities1();
            ybd.AddObject(entityname, data);
            ybd.SaveChanges();
        }
        public static void Xoa(object data)
        {
            ybdEntities1 ybd = new ybdEntities1();
            ybd.DeleteObject(data);
            ybd.SaveChanges();
        }

    }
}
